package com.wsq.listener;

import com.wsq.calculate.R;
import com.wsq.function.DisplayLogic;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class EventListener implements OnClickListener, OnKeyListener,
		OnLongClickListener, OnCheckedChangeListener {

	private static final char[] EQUAL = { '=' };
	private DisplayLogic mHandler;

	private Button mLnButton;
	private Button mIntButton;
	private Button mPiButton;
	private Button mSinButton;
	private Button mCosButton;
	private Button mTanButton;
	private Button mSinhButton;
	private Button mCoshButton;
	private Button mTanhButton;

	private CheckBox mInvCheckBox;
	
	private int mOrientation;
	
	private Activity mContext;

	public EventListener(DisplayLogic mHandler) {
		super();
		this.mHandler = mHandler;
	}

	public EventListener() {
		// TODO Auto-generated constructor stub
	}

	public void setLogic(DisplayLogic logic) {
		this.mHandler = logic;
	}
	
	public void setOrientation(int orientation){
		this.mOrientation=orientation;
	}

	public void setWidgets(Activity activity) {
		this.mContext=activity;
		mLnButton=(Button)activity.findViewById(R.id.idKeyLn);
		mCosButton = (Button) activity.findViewById(R.id.idKeyCos);
		mCoshButton = (Button) activity.findViewById(R.id.idKeyCosh);
		mIntButton = (Button) activity.findViewById(R.id.idKeyInt);
		mPiButton = (Button) activity.findViewById(R.id.idKeyPI);
		mSinButton = (Button) activity.findViewById(R.id.idKeySin);
		mSinhButton = (Button) activity.findViewById(R.id.idKeySinh);
		mTanButton = (Button) activity.findViewById(R.id.idKeyTan);
		mTanhButton = (Button) activity.findViewById(R.id.idKeyTanh);

		mInvCheckBox = (CheckBox) activity.findViewById(R.id.idKeyInv);
	}

	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		return false;
	}

	// �Լ��ɷ��İ���
	// ��Ӧ����
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		int action = event.getAction();

		// When dpad left,then when the cursor come to 0 position
		// it will return true,the listener has consumed the event
		// return false,the cursor will move to left default
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT
				|| keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			boolean eat = mHandler
					.eatHorizontalMove(keyCode == KeyEvent.KEYCODE_DPAD_LEFT);
			return eat;
		}

		// Work-around for spurious key event from IME, bug #1639445
		if (action == KeyEvent.ACTION_MULTIPLE
				&& keyCode == KeyEvent.KEYCODE_UNKNOWN) {
			return true; // eat it
		}

		// Calculator.log("KEY " + keyCode + "; " + action);

		if (event.getMatch(EQUAL, event.getMetaState()) == '=') {
			if (action == KeyEvent.ACTION_UP) {
				mHandler.onEnter();
			}
			return true;
		}

		if (keyCode != KeyEvent.KEYCODE_DPAD_CENTER
				&& keyCode != KeyEvent.KEYCODE_DPAD_UP
				&& keyCode != KeyEvent.KEYCODE_DPAD_DOWN
				&& keyCode != KeyEvent.KEYCODE_ENTER) {
			return false;
		}

		/*
		 * We should act on KeyEvent.ACTION_DOWN, but strangely sometimes the
		 * DOWN event isn't received, only the UP. So the workaround is to act
		 * on UP... http://b/issue?id=1022478
		 */

		if (action == KeyEvent.ACTION_UP) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_ENTER:
			case KeyEvent.KEYCODE_DPAD_CENTER:
				mHandler.onEnter();
				break;

			case KeyEvent.KEYCODE_DPAD_UP:
				mHandler.onUp();
				break;

			case KeyEvent.KEYCODE_DPAD_DOWN:
				mHandler.onDown();
				break;
			}
		}
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		com.wsq.global.Configuration.instance().playVibrate(mContext);
		switch (v.getId()) {
		// MC
		case R.id.idKeyMC:
			break;
		// MR
		case R.id.idKeyMR:
			break;
		// MS
		case R.id.idKeyMS:
			break;
		// M+
		case R.id.idKeyMPlus:
			break;
		// M-
		case R.id.idKeyMMinus:
			break;
		// DEL
		case R.id.idKeyDel:
			mHandler.onDelete();
			break;
		// CE
		case R.id.idKeyCE:
			mHandler.onClear();
			break;
		// C
		case R.id.idKeyC:
			mHandler.onClear();
			break;
		case R.id.idKeyXPowY:
			mHandler.insert("^");
			break;
		case R.id.idKeyXRootY:
			mHandler.insert("root");
			break;
		case R.id.idKeyExp:
			mHandler.insert("e");
			break;
		case R.id.idKeyLn:
		case R.id.idKeyInt:
		case R.id.idKeyPI:
		case R.id.idKeySinh:
		case R.id.idKeyCosh:
		case R.id.idKeyTanh:
		case R.id.idKeySin:
		case R.id.idKeyCos:
		case R.id.idKeyTan:
			String temp_text = ((Button) v).getText().toString();
			mHandler.insert(temp_text);
			onCheckedChanged(mInvCheckBox, false);
			break;
		default:
			if (v instanceof Button) {
				String text = ((Button) v).getText().toString();
				// if (text.length() >= 2) {
				// text += "(";
				// }
				mHandler.insert(text);
			}
			break;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		if (isChecked) {
			buttonView.setBackgroundResource(R.drawable.frame_check);
			mLnButton.setText("e^x");
			mLnButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?26:24);
			mIntButton.setText("Frac");
			mIntButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?23:18);
			mPiButton.setText("x*π");
			mPiButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?23:18);
			mSinhButton.setText("asinh");
			mSinhButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?18:16);
			mCoshButton.setText("acosh");
			mCoshButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?18:16);
			mTanhButton.setText("atanh");
			mTanhButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?18:16);
			mSinButton.setText("asin");
			mSinButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?23:18);
			mCosButton.setText("acos");
			mCosButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?23:18);
			mTanButton.setText("atan");
			mTanButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?23:18);
		} else {
			buttonView.setBackgroundResource(R.drawable.frame_special);
			mLnButton.setText("ln");
			mLnButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?30:26);
			mIntButton.setText("Int");
			mIntButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?26:20);
			mPiButton.setText("π");
			mPiButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 26);
			mSinhButton.setText("sinh");
			mSinhButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?24:20);
			mCoshButton.setText("cosh");
			mCoshButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?24:20);
			mTanhButton.setText("tanh");
			mTanhButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?24:20);
			mSinButton.setText("sin");
			mSinButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?26:20);
			mCosButton.setText("cos");
			mCosButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?26:20);
			mTanButton.setText("tan");
			mTanButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, mOrientation==Configuration.ORIENTATION_PORTRAIT?26:20);
		}
	}

}
